/**
 * @fileName TestChildController.java
 * @package com.senvon.newApp.controller.child
 * @copyright Copyright xie xianchao All Rights Reserved.
 * @description 
 * @version 1.0
 * @author xie xianchao
 * @date 2017年6月8日
 */
package com.senvon.newApp.controller.child;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.istock.base.ibatis.model.Page;
import com.senvon.newApp.model.MenuInfo;
import com.senvon.newApp.model.TestChild;
import com.senvon.newApp.model.TestChildExample;
import com.senvon.newApp.service.child.TestChildService;

/**
 * 
 * @author xie xianchao
 * @version 1.0
 * @date 2017年6月8日
 */
@Controller
@RequestMapping("child")
public class TestChildController {
	
	/**
	 * org.slf4j.Logger </br>Logger org.slf4j.LoggerFactory.getLogger(Class clazz)
	 */
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private TestChildService testChildService;
	
	@Value("${testChild.title}")
	private String title;
	
	/**
	 * 分页查询列表
	 * 
	 * @param model
	 * @param name
	 * @param page
	 * @return
	 */
	@RequestMapping("showChildList")
	public String showChildList(ModelMap model, String name, Page<TestChild> page){
		logger.info("showChildList ====:{}, {}", name, page);
		if(page == null){
			page = new Page<TestChild>();
		}
		page.setPageSize(3);
		TestChildExample example = new TestChildExample();
		if(name != null && !name.isEmpty()){
			TestChildExample.Criteria criteria = example.createCriteria();
			criteria.andNameLike("%" + name + "%");
		}
		page = testChildService.selectByPage(example, page);
		model.put("name", name);
		model.put("page", page);
		model.put("title", title);
		return "child/childList";
	}
	
	/**
	 * 进入编辑页面，新增、修改通用
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("editPre")
	public String editPre(ModelMap model, Integer id){
		logger.info("editPre ====:{}", id);
		if(id != null && id > 0){
			//update
			TestChild testChild = testChildService.selectByPrimaryKey(id);
			model.put("testChild", testChild);
		}
		model.put("page_title", "编辑");
		return "child/edit";
	}
	
	/**
	 * 编辑，新增or修改
	 * 
	 * @param model
	 * @param testChild
	 * @return
	 */
	@RequestMapping("edit")
	public String edit(ModelMap model, TestChild testChild){
		logger.info("edit =====: {}", testChild);
		int result = 0;
		if(testChild != null && testChild.getId() != null){			
			result = testChildService.updateByPrimaryKeySelective(testChild);
		}else{
			result = testChildService.insert(testChild);
		}
		if(result > 0){
			model.put("message", "保存成功");
		}else{
			model.put("message", "保存失败");
			model.put("testChild", testChild);
			return "child/edit";
		}
		return showChildList(model, null, new Page());
	}
	
	/**
	 * 删除
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	public String delete(ModelMap model, Integer id){
		int result = 0;
		if(id != null){
			testChildService.deleteByPrimaryKey(id);
		}
		if(result > 0){
			model.put("message", "保存成功");
		}else{
			model.put("message", "保存失败");
		}
		return showChildList(model, null, new Page());
	}
	
}
